package com.canbot.u05.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnSystemUiVisibilityChangeListener;
import android.view.Window;
import android.view.WindowManager;

import com.canbot.u05.R;
import com.canbot.u05.helper.DialogBuildHelper;
import com.canbot.u05.utils.StatusBarUtil;


public class BaseActivityNoTitle extends Activity {

        private DialogBuildHelper.HintTipBuilder hintTip;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                        WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                hideBottomUIMenu();
        }

        @Override
        protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();
                hideBottomUIMenu();
        }

        @Override
        protected void onPause() {
                super.onPause();
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
                // TODO Auto-generated method stub
                //	        switch(ev.getAction()){
                //	            case MotionEvent.ACTION_DOWN:
                //	                Logger.v("BaseActivityNoTitle", "dispatchTouchEvent");
                //	                synchronized (MainApplication.LOCK) {
                //	                        ((MainApplication)getApplication()).touchTime = System.currentTimeMillis();
                //	                    }
                //	                break;
                //	            }
                return super.dispatchTouchEvent(ev);
        }


        /**
         * 隐藏虚拟按键，并且全屏
         */
        protected void hideBottomUIMenu() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
                getWindow().setAttributes(attributes);
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
                        @Override
                        public void onSystemUiVisibilityChange(int visibility) {
                                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                                        //布局位于状态栏下方
                                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                                        //全屏
                                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                                        //隐藏导航栏
                                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                                uiOptions |= 0x00001000;
                                getWindow().getDecorView().setSystemUiVisibility(uiOptions);
                        }
                });
        }

        protected void startOtherActivity(Class<? extends Activity> clazz, boolean isFinish) {
                startOtherActivity(new Intent(this, clazz), isFinish);
        }

        protected void startOtherActivity(Intent intent, boolean isFinish) {
                startActivity(intent);
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                if (isFinish) {
                        finish();
                }
        }

        protected void startForResultActivity(Intent intent, int code) {
                startActivityForResult(intent, code);
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
        }

        protected void startForResultActivity(Class<? extends Activity> clazz, int code) {
                startForResultActivity(new Intent(this, clazz), code);
        }

        private Dialog loadDialog = null;

        protected void showBaseLoadDialog() {
                runOnUiThread(() -> {
                        if (loadDialog == null) {
                                loadDialog = new Dialog(BaseActivityNoTitle.this, R.style.MyDialogStyle);
                                loadDialog.setContentView(R.layout.guide_dialog_load);
                                loadDialog.setCancelable(false);
                        }
                        StatusBarUtil.hideVirtualKey(loadDialog);
                        loadDialog.show();
                });

        }

        protected void dismissBaseDialog() {
                if (loadDialog != null) {
                        loadDialog.dismiss();
                }
        }

        protected void showBaseHintDialog(String content,Boolean finishActivity) {
                runOnUiThread(() -> {
                        if (hintTip == null) {
                                hintTip = DialogBuildHelper.createHintTipDialog(BaseActivityNoTitle.this);

                        }
                        hintTip.setClickListener(() -> {
                                if (finishActivity) {
                                        finish();
                                }
                        },true).setContent(content, Gravity.CENTER).show();
                });

        }

        protected void showBaseHintDialog(String content) {
                showBaseHintDialog(content,false);
        }

}
